//
//  EventAddController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/26/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "EventAddController.h"
@interface EventAddController ()

@end

@implementation EventAddController

@synthesize viewAdd, iconCurrency, iconName, iconPaid, txtName, btCurrency, btAddMember, btPaid, currencyPicker;

@synthesize listCurrency, currencyId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.listCurrency = [[NSMutableArray alloc] init];
    [self changeStyle];
    [self createNavigation];
    [self createPopup];
    [self hideDatePicker];
    [self loadData];
}

-(void) changeStyle
{
    [StyleControl background:self.view];
    [StyleControl setViewStyle:self.viewAdd];
    [StyleControl drawLine:self.viewAdd PositionX:0 PositionY:45 Width:306];
    [StyleControl drawLine:self.viewAdd PositionX:0 PositionY:90 Width:306];
    UIImageView *image = [[UIImageView alloc] init];
    image.frame = CGRectMake(4,7, 30,26);
    [image setImage:[UIImage imageNamed:@"event"]];
    [viewAdd addSubview:image];
    [image release];
}
-(void) createNavigation
{
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *right =[UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"buttonlist"] forState:UIControlStateNormal];
    left.frame = CGRectMake(8, 8, 37, 35);
    right.frame = CGRectMake(250, 8, 60, 35);
    [right setTitle:@"Gereed" forState:UIControlStateNormal];
    NSString *title = [NSString stringWithFormat:NSLocalizedString(@"event.add.title", @"")];
    [left addTarget:self action:@selector(openEventList) forControlEvents:UIControlEventTouchUpInside];
    [right addTarget:self action:@selector(addMember:) forControlEvents:UIControlEventTouchUpInside];
    [StyleControl naviStyle:self.view andTitle:title andColor:0 andleftButton:left andRightButton:right borderLeft:1 borderRight:1];
}
-(void) createPopup{
    UIView *pickerView = [[UIView alloc] initWithFrame:CGRectMake(50, 140, 200, 150)];
    UIToolbar *datePickerTimerToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
    datePickerTimerToolbar.barStyle=UIBarStyleBlackTranslucent;
    
    
    NSArray *barPickerTimerItems = [NSArray arrayWithObjects:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease], [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideDatePicker)] autorelease], nil];
    
    [datePickerTimerToolbar setItems:barPickerTimerItems animated:YES];
    [pickerView addSubview:self.currencyPicker];
    [pickerView addSubview:datePickerTimerToolbar];
    [self.view addSubview:pickerView];
    [pickerView setHidden:NO];
    currencyPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 200, 110)];
	
	currencyPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
	//CGSize pickerSize = [myPickerView sizeThatFits:CGSizeZero];
	currencyPicker.frame = CGRectMake(0, 40, 200, 162);
    
	currencyPicker.showsSelectionIndicator = YES;	// note this is default to NO
	
	// this view controller is the data source and delegate
	currencyPicker.delegate = self;
	currencyPicker.dataSource = self;
	
	// add this picker to our view controller, initially hidden
    currencyPicker.hidden = NO;
	[pickerView addSubview:currencyPicker];
}

-(void)hideDatePicker
{
    [self.currencyPicker setHidden:YES];
    [self.currencyPicker.superview setHidden:YES];
}
-(void)showDatePicker
{
    [self.currencyPicker setHidden:NO];
    [self.currencyPicker.superview setHidden:NO];
}

-(void) loadData
{
    Currency *currency = [Currency instance];
    self.listCurrency = [currency getList];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // Any additional checks to ensure you have the correct textField here.
    [textField resignFirstResponder];
    return YES;
}

-(void) openEventList
{
    EventListController *detail = [[EventListController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}

-(IBAction)addCurrency:(id)sender
{
    [self.txtName resignFirstResponder];
    [self showDatePicker];
    [self pickerView:currencyPicker didSelectRow:0 inComponent:0];
}

-(IBAction)addPaid:(id)sender
{
    
}

-(IBAction)addMember:(id)sender
{
    Event *event = [Event instance];
    event._name = self.txtName.text;
    event.currencyId = self.currencyId;
    event.currency = self.btCurrency.titleLabel.text;
    EventAddMemberController *detail = [[EventAddMemberController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}

#pragma mark -
#pragma mark UIPickerViewDataSource

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	CurrencyClass *cur;
    cur = [self.listCurrency objectAtIndex:row];
    
    NSString *returnStr = cur.name;
	
	
    return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.listCurrency count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}
-(void) pickerView:(CPPickerView *)pickerView didSelectItem:(NSInteger)row
{
   
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    CurrencyClass *cur;
    cur = [self.listCurrency objectAtIndex:row];
    self.currencyId = cur.currencyId;
    [btCurrency setTitle:cur.name forState:UIControlStateNormal];
}
-(NSString *)pickerView:(CPPickerView *)pickerView titleForItem:(NSInteger)item
{
    return @"item";
}
-(NSInteger) numberOfItemsInPickerView:(CPPickerView *)pickerView
{
    return [self.listCurrency count];
}
@end
